package com.aibaixun.web.controller.system;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.aibaixun.common.config.ValidGroup;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.system.domain.PostureDevice;
import com.aibaixun.system.domain.dto.PostureDeviceDto;
import com.aibaixun.system.domain.dto.PostureDevicePageDto;
import com.aibaixun.system.domain.vo.ExportDeviceVo;
import com.aibaixun.system.domain.vo.PostureDeviceVo;
import com.aibaixun.system.service.PostureDeviceService;
import com.aibaixun.web.controller.common.UploadFileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 架式设备上下架管理模块
 *
 * @author zhangyh
 * @date 2023-09-01
 **/
@RestController
@RequestMapping("/posture/device" )
public class PostureDeviceController {

    @Autowired
    private PostureDeviceService postureDeviceService;

    @Autowired
    private UploadFileUtil uploadFileUtil;

    /**
     * 分页查询架式设备
     * @return
     */
    @ApiOperation(value = "分页查询架式设备")
    @PostMapping("/page")
    public R<Page<PostureDevice>> getPage(@RequestBody PostureDevicePageDto pageDto){
        Page page = new Page();
        page.setCurrent(pageDto.getCurrent());
        page.setSize(pageDto.getSize());
        page.setSearchCount(true);
        return postureDeviceService.getPage(page, pageDto);
    }

    /**
     * 查询架式设备列表
     * @return
     */
    @ApiOperation(value = "查询架式设备列表")
    @PostMapping("/list")
    public R<List<PostureDeviceVo>> getList(@RequestBody PostureDeviceDto dto){
        return postureDeviceService.getList(dto);
    }

    /**
     * 新增架式设备
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody @Validated(ValidGroup.Add.class) PostureDeviceDto dto) {
        return postureDeviceService.add(dto);
    }

    /**
     * 更新架式设备
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody @Validated(ValidGroup.Update.class) PostureDeviceDto dto) {
        return postureDeviceService.update(dto);
    }

    /**
     * 删除架式设备
     * @return
     */
    @GetMapping("/delete/{deviceId}")
    public R delete(@PathVariable(value = "deviceId") Long deviceId) {
        return postureDeviceService.delete(deviceId);
    }


    /**
     * 根据ueid查询机柜及u信息
     * @param cabinetId
     * @return
     */
//    @GetMapping("/query/byCabinetId")
//    public R queryByRoomId(@RequestParam String cabinetId){
//        List<PostureDevice> deviceList = postureDeviceService.queryByRoomId(cabinetId);
//        return R.ok(deviceList);
//    }

    /**
     * 上传U信息
     * @param file
     * @return
     */
    @PostMapping("/upload/deviceFile")
    public R uploadDeviceFile(@RequestParam("file") MultipartFile file) throws IOException {
        List<PostureDevice> postureDeviceList = uploadFileUtil.uploadFileList(file);
        return postureDeviceService.saveDeviceList(postureDeviceList);
    }

    /**
     * 导出u信息列表
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        //查询所有u信息
        List<ExportDeviceVo> deviceList = postureDeviceService.exportDevice();

        //创建xlsx格式的
        ExcelWriter writer = ExcelUtil.getWriter();
        //自定义标题别名
        writer.addHeaderAlias("name","设备名称");
        writer.addHeaderAlias("model","设备型号");
        writer.addHeaderAlias("cabinetUeid","设备所属机柜唯一标识");
        writer.addHeaderAlias("cabinetNum","设备所属机柜");
        writer.addHeaderAlias("location","起始");
        writer.addHeaderAlias("height","U高");

        //默认配置
        writer.write(deviceList,true);
        //设置content—type
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        //设置标题
        String fileName= URLEncoder.encode("IT资产数据统计","UTF-8");
        //Content-disposition是MIME协议的扩展，MIME协议指示MIME用户代理如何显示附加的文件。
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
        ServletOutputStream outputStream= response.getOutputStream();

        //将Writer刷新到OutPut
        writer.flush(outputStream,true);
        outputStream.close();
        writer.close();
    }


}
